package com.haotunet.app.goldtreasure.activity;

import java.util.ArrayList;
import java.util.List;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.MainPagerAdapter;
import com.haotunet.app.goldtreasure.fragment.DiscountFragment;
import com.haotunet.app.goldtreasure.views.TitleView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class DiscountActivity extends BaseActivity implements OnPageChangeListener {
	private TitleView mTitleView;
	private ViewPager mViewpager;
	private MainPagerAdapter adapter;
	private TextView tv_menu[];
	private Fragment fragment[];
	private ImageView ivUnuserdLine, ivHistoryLine;
	private List<ImageView> ivList;
	private int prePosition = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_discount);
		initView();
	}

	private void initView() {
		mTitleView = (TitleView) findViewById(R.id.title_view);
		mTitleView.getTitletextView().setVisibility(View.VISIBLE);
		mTitleView.getTitletextView().setText(getString(R.string.coupon));
		mTitleView.getBackView().setVisibility(View.VISIBLE);
		mTitleView.getBackView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		ivUnuserdLine = (ImageView) findViewById(R.id.iv_unuserd_line);
		ivHistoryLine = (ImageView) findViewById(R.id.iv_history_line);
		mViewpager = (ViewPager) findViewById(R.id.vp_discount);
		tv_menu = new TextView[2];
		tv_menu[0] = (TextView) findViewById(R.id.tv_discount_unused);
		tv_menu[1] = (TextView) findViewById(R.id.tv_discount_history);
		for (int i = 0; i < tv_menu.length; i++) {
			final int cur = i;
			tv_menu[i].setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					onClickIndex(cur);
				}

			});
		}
		fragment = new Fragment[2];
		fragment[0] = DiscountFragment.newInstance(1);
		fragment[1] = DiscountFragment.newInstance(2);
		ivList = new ArrayList<ImageView>();
		ivList.add(ivUnuserdLine);
		ivList.add(ivHistoryLine);
		adapter = new MainPagerAdapter(getSupportFragmentManager());
		adapter.add(fragment[0]);
		adapter.add(fragment[1]);
		mViewpager.setAdapter(adapter);
		mViewpager.setOffscreenPageLimit(adapter.getCount());
		mViewpager.addOnPageChangeListener(this);
		onClickIndex(0);
	}

	public void onClickIndex(int cur) {
		mViewpager.setCurrentItem(cur, false);
		for (int i = 0; i < tv_menu.length; i++) {

			if (cur == i) {
				tv_menu[i].setSelected(true);
			} else {
				tv_menu[i].setSelected(false);
			}

		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@SuppressWarnings("deprecation")
	@Override
	public void onPageSelected(int arg0) {
		ivList.get(prePosition).setBackgroundColor(getResources().getColor(R.color.color_white));
		ivList.get(arg0).setBackgroundColor(getResources().getColor(R.color.yellow_btn_bg));
		tv_menu[arg0].setTextColor(getResources().getColor(R.color.color_yellow));
		tv_menu[prePosition].setTextColor(getResources().getColor(R.color.color_black));
		prePosition = arg0;
	}
}
